package team.nmsg.ge.system.bean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import team.nmsg.ge.system.interf.ISysBean;

import com.alibaba.fastjson.JSONObject;

@Entity
@Table( uniqueConstraints={@UniqueConstraint(columnNames={"username"})  }  )
public class SysUser implements ISysBean{

	public transient static int USER_STATUS_NORMAL = 1;
	public transient static int USER_STATUS_ABNORMAL = 2;
	
	//主键
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer userId;
	//username
	private String username;
	// password
	private String password;
	//描述与备注
	private String userRemark;
	//状态  正常1 冻结2  
	private Integer userStatus = USER_STATUS_NORMAL;
	//创建时间戳
	private Long createTimestamp;
	//最近一次修改时间戳
	private Long lastUpdateTimestamp;
	
	//最近一次修改时间戳
	private Long lastLoginTimestamp;
	private Long loginCounter;
	
	private String phoneNo;
	private String email;
	
	//保留字段
	private String standby1;
	private String standby2;
	
    @ManyToMany(fetch=FetchType.EAGER )
    @JoinTable(name="SYS_U_R",joinColumns=@JoinColumn(name="u_id"), inverseJoinColumns=@JoinColumn(name="r_id"))
	private Set<SysRole> roles ;
	
	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserRemark() {
		return userRemark;
	}

	public void setUserRemark(String userRemark) {
		this.userRemark = userRemark;
	}

	public Integer getUserStatus() {
		return userStatus;
	}

	public void setUserStatus(Integer userStatus) {
		this.userStatus = userStatus;
	}

	public Long getCreateTimestamp() {
		return createTimestamp;
	}

	public void setCreateTimestamp(Long createTimestamp) {
		this.createTimestamp = createTimestamp;
	}

	public Long getLastUpdateTimestamp() {
		return lastUpdateTimestamp;
	}

	public void setLastUpdateTimestamp(Long lastUpdateTimestamp) {
		this.lastUpdateTimestamp = lastUpdateTimestamp;
	}

	public String getStandby1() {
		return standby1;
	}

	public void setStandby1(String standby1) {
		this.standby1 = standby1;
	}

	public String getStandby2() {
		return standby2;
	}

	public void setStandby2(String standby2) {
		this.standby2 = standby2;
	}

	public Set<SysRole> getRoles() {
		return roles;
	}

	public void setRoles(Set<SysRole> roles) {
		this.roles = roles;
	}
	
	public Long getLastLoginTimestamp() {
		return lastLoginTimestamp;
	}

	public void setLastLoginTimestamp(Long lastLoginTimestamp) {
		this.lastLoginTimestamp = lastLoginTimestamp;
	}

	public Long getLoginCounter() {
		return loginCounter;
	}

	public void setLoginCounter(Long loginCounter) {
		this.loginCounter = loginCounter;
	}

	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public JSONObject toJson() {
		JSONObject ob = new JSONObject();
		ob.put("sysUserStatus", userStatus);
		ob.put("userId", userId);
		ob.put("username",username);
		ob.put("password", password);
		ob.put("userRemark", userRemark==null?"" : userRemark);
		ob.put("createTimestamp", formatDate(createTimestamp) );
		ob.put("lastUpdateTimestamp", formatDate(lastUpdateTimestamp));
		ob.put("lastLoginTime", lastLoginTimestamp==null?"" : formatDate(lastLoginTimestamp));
		ob.put("loginNum", loginCounter);
		ob.put("email", email == null ? "" : email);
		ob.put("phone", phoneNo == null ? "" : phoneNo);
		return ob;
	}
	
	
	public String formatDate(Long timestamp){
		
		if( timestamp ==null ){
			return "";
		}
		
		Date date = new Date(timestamp);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
	
}
